package org.hilo.boot.core.logback;

import java.io.Serializable;

import org.apache.juli.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author zollty
 * @since 2022年7月18日
 */
public class Slf4jLogger<T extends Logger> implements Log, Serializable {
    private static final long serialVersionUID = 1960766346396566392L;

    protected final String name;

    protected transient T logger;
    
    public Slf4jLogger() {
        this.name = null;
    }

    public Slf4jLogger(T logger) {
        this.name = logger.getName();
        this.logger = logger;
    }

    @Override
    public boolean isFatalEnabled() {
        return isErrorEnabled();
    }

    @Override
    public boolean isErrorEnabled() {
        return this.logger.isErrorEnabled();
    }

    @Override
    public boolean isWarnEnabled() {
        return this.logger.isWarnEnabled();
    }

    @Override
    public boolean isInfoEnabled() {
        return this.logger.isInfoEnabled();
    }

    @Override
    public boolean isDebugEnabled() {
        return this.logger.isDebugEnabled();
    }

    @Override
    public boolean isTraceEnabled() {
        return this.logger.isTraceEnabled();
    }

    @Override
    public void fatal(Object message) {
        error(message);
    }

    @Override
    public void fatal(Object message, Throwable exception) {
        error(message, exception);
    }

    @Override
    public void error(Object message) {
        if (message instanceof String || this.logger.isErrorEnabled()) {
            this.logger.error(String.valueOf(message));
        }
    }

    @Override
    public void error(Object message, Throwable exception) {
        if (message instanceof String || this.logger.isErrorEnabled()) {
            this.logger.error(String.valueOf(message), exception);
        }
    }

    @Override
    public void warn(Object message) {
        if (message instanceof String || this.logger.isWarnEnabled()) {
            this.logger.warn(String.valueOf(message));
        }
    }

    @Override
    public void warn(Object message, Throwable exception) {
        if (message instanceof String || this.logger.isWarnEnabled()) {
            this.logger.warn(String.valueOf(message), exception);
        }
    }

    @Override
    public void info(Object message) {
        if (message instanceof String || this.logger.isInfoEnabled()) {
            this.logger.info(String.valueOf(message));
        }
    }

    @Override
    public void info(Object message, Throwable exception) {
        if (message instanceof String || this.logger.isInfoEnabled()) {
            this.logger.info(String.valueOf(message), exception);
        }
    }

    @Override
    public void debug(Object message) {
        if (message instanceof String || this.logger.isDebugEnabled()) {
            this.logger.debug(String.valueOf(message));
        }
    }

    @Override
    public void debug(Object message, Throwable exception) {
        if (message instanceof String || this.logger.isDebugEnabled()) {
            this.logger.debug(String.valueOf(message), exception);
        }
    }

    @Override
    public void trace(Object message) {
        if (message instanceof String || this.logger.isTraceEnabled()) {
            this.logger.trace(String.valueOf(message));
        }
    }

    @Override
    public void trace(Object message, Throwable exception) {
        if (message instanceof String || this.logger.isTraceEnabled()) {
            this.logger.trace(String.valueOf(message), exception);
        }
    }

    protected Object readResolve() {
        return new Slf4jLogger<>(LoggerFactory.getLogger(this.name));
    }

}
